package com.chrilwe.pigpig.filesystem.common.util;
/**
 * HDFS文件系统工具
 * @author chrilwe
 * @date 2020-10-4
 */

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class HdfsFileSystemUtil {
	
	private static final String address = "hdfs://192.168.43.163:9000";
	
	//上传文件
	public static void upload(Path src, Path dst) throws IOException {
		FileSystem fs = getFs();
		fs.copyFromLocalFile(src, dst);
	}
	
	//下载文件
	public static FSDataInputStream download(Path f) throws IOException {
		FileSystem fs = getFs();
		return fs.open(f);
	}
	
	protected static FileSystem getFs() {
		try {
			URI uri = new URI(address);
			Configuration conf = new Configuration();
			FileSystem fs = FileSystem.get(uri, conf);
			return fs;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
